/**
 * 
 */
package leetCode;

/**
 * @author zhong
 *
 */
public class NumberOfCornerRectangles {
	public int countCornerRectangles(int[][] grid) {
		int n = grid.length;
		int m = grid[0].length;
		int count = 0;
		for (int i = 0; i < n; i++) {
			for (int p = 0; p < n; p++) {
				int c = 0;
				for (int k = 0; k < m; k++) {
					if (grid[i][k] == 1 && grid[p][k] == 1)
						c++;
				}
				count += (c - 1) * c / 2;
			}
		}
		return count;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int[][] grid = { { 1, 0, 0, 1, 0 }, { 0, 0, 1, 0, 1 }, { 0, 0, 0, 1, 0 }, { 1, 0, 1, 0, 1 } };
		System.out.println(new NumberOfCornerRectangles().countCornerRectangles(grid));
	}

}
